<?php
require_once('inc.config.php');

# Contents may be empty in case of null/blank files.
if(!isset($_POST['contents'])) die('Missing file contents.');
$contents = $_POST['contents'];

# Save to file name must exist.
if(empty($_POST['file'])) die('Missing file name to save.');
$file = base64_decode($_POST['file']);
#if(!is_file($file)) die('Cannot overwrite the contents in '.$file);

# Finally save the file
if(is_writable($file))
{
	file_put_contents($file, $contents, FILE_BINARY);
}

# Debugging notes
# header('Content-Type: text/plain'); die("Writing to: {$file}\r\n --- \r\n{$contents}");

$go_back = !empty($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'./?saved=true';
header("Location: {$go_back}", true, 301);
?>